import asyncio
import base64
import json
import queue
from voice_assistant.my_state import app_state

async def send_audio_data(ws):
    """异步发送音频数据"""
    while True:
        try:
            data = app_state.audio_queue.get(timeout=0.05)
            if data:
                await ws.send(json.dumps({
                    'type': 'full_audio',
                    'data': base64.b64encode(data).decode('utf-8'),
                    'sample_rate': 16000
                }))
        except queue.Empty:
            await asyncio.sleep(0.01)
        except Exception as e:
            print(f"音频数据发送失败: {str(e)}")
            break
    app_state.server_connected = False